package com.yserp.project.erp.basic.goodstype.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yserp.common.constant.Constants;
import com.yserp.common.utils.StringUtils;
import com.yserp.common.utils.mybatis.MybatisPlusUtil;
import com.yserp.framework.web.domain.Ztree;
import com.yserp.project.erp.basic.goodstype.domain.BasisGoodsType;
import com.yserp.project.erp.basic.goodstype.mapper.BasisGoodsTypeMapper;
import com.yserp.project.erp.basic.goodstype.service.IBasisGoodsTypeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 商品类别Service业务层处理
 *
 * @author yserp
 * @date 2022-06-29
 */
@Service
public class BasisGoodsTypeServiceImpl extends ServiceImpl<BasisGoodsTypeMapper,BasisGoodsType> implements IBasisGoodsTypeService {
    @Resource
    private BasisGoodsTypeMapper basisGoodsTypeMapper;

    /**
     * 查询商品类别
     *
     * @param id 商品类别主键
     * @return 商品类别
     */
    @Override
    public BasisGoodsType selectBasisGoodsTypeById(Long id) {
        return basisGoodsTypeMapper.selectById(id);
    }

    /**
     * 查询商品类别列表
     *
     * @param basisGoodsType 商品类别
     * @return 商品类别
     */
    @Override
    public List<BasisGoodsType> selectBasisGoodsTypeList(BasisGoodsType basisGoodsType) {
        QueryWrapper<BasisGoodsType> queryWrapper = new QueryWrapper<>();
        MybatisPlusUtil.notNullField(basisGoodsType,queryWrapper);
        return basisGoodsTypeMapper.selectList(queryWrapper);
    }

    /**
     * 新增商品类别
     *
     * @param basisGoodsType 商品类别
     * @return 结果
     */
    @Override
    public int insertBasisGoodsType(BasisGoodsType basisGoodsType) {

        Long parentId = basisGoodsType.getParentId();
        if (StringUtils.isNotNull(parentId)) {
            BasisGoodsType info = basisGoodsTypeMapper.selectById(parentId);
            basisGoodsType.setAncestors(info.getAncestors() + "," + parentId);
        } else {
            basisGoodsType.setParentId(0L);
            basisGoodsType.setAncestors("0");
        }
        return basisGoodsTypeMapper.insert(basisGoodsType);
    }

    /**
     * 修改商品类别
     *
     * @param basisGoodsType 商品类别
     * @return 结果
     */
    @Override
    public int updateBasisGoodsType(BasisGoodsType basisGoodsType) {
        BasisGoodsType newGoodsType = basisGoodsTypeMapper.selectById(basisGoodsType.getParentId());
        BasisGoodsType oldGoodsType = basisGoodsTypeMapper.selectById(basisGoodsType.getId());
        if (StringUtils.isNotNull(newGoodsType) && StringUtils.isNotNull(oldGoodsType)) {
            String newAncestors = newGoodsType.getAncestors() + "," + newGoodsType.getId();
            String oldAncestors = oldGoodsType.getAncestors();
            basisGoodsType.setAncestors(newAncestors);
            updateGoodsTypeChildren(basisGoodsType.getId(), newAncestors, oldAncestors);
        }
        return basisGoodsTypeMapper.updateById(basisGoodsType);
    }

    /**
     * 修改子元素关系
     *
     * @param typeId       被修改的商品类别ID
     * @param newAncestors 新的父ID集合
     * @param oldAncestors 旧的父ID集合
     */
    public void updateGoodsTypeChildren(Long typeId, String newAncestors, String oldAncestors) {
        List<BasisGoodsType> children =
                basisGoodsTypeMapper.selectList(new QueryWrapper<BasisGoodsType>().apply(typeId != null,"FIND_IN_SET ("+typeId+",ancestors)"));
        for (BasisGoodsType child : children) {
            child.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors));
        }
        if (children.size() > 0) {
            basisGoodsTypeMapper.updateGoodsTypeChildren(children);
        }
    }



    /**
     * 删除商品类别信息
     *
     * @param id 商品类别主键
     * @return 结果
     */
    @Override
    public int deleteBasisGoodsTypeById(Long id) {
        return basisGoodsTypeMapper.deleteById(id);
    }

    /**
     * 查询商品类别树列表
     *
     * @return 所有商品类别信息
     */
    @Override
    public List<Ztree> selectGoodsTypeTree() {
        //获取总数据
        List<BasisGoodsType> shopGoodsTypeList = this.selectBasisGoodsTypeList(new BasisGoodsType());
        List<Ztree> ztrees = new ArrayList<Ztree>();
        for (BasisGoodsType type : shopGoodsTypeList) {
            Ztree ztree = new Ztree();
            ztree.setId(type.getId());
            ztree.setpId(type.getParentId());
            ztree.setName(type.getGoodsTypeName());
            ztree.setTitle(type.getGoodsTypeName());
            ztrees.add(ztree);
        }
        return ztrees;
    }

    @Override
    public String checkGoodsTypeCodeUnique(String goodsTypeCode) {
        String flag = Constants.NAME_UNIQUE;
        int count = basisGoodsTypeMapper.selectCount(new QueryWrapper<BasisGoodsType>().eq("goods_type_code",goodsTypeCode));
        if (count > 0) {
            flag = Constants.NAME_NOT_UNIQUE;
        }
        return flag;
    }

    @Override
    public int selectGoodsTypeCount(Long parentId) {
        return basisGoodsTypeMapper.selectCount(new QueryWrapper<BasisGoodsType>().eq("parent_id",parentId));
    }
}
